package com.top.art.utils;


import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.sts.model.v20150401.AssumeRoleRequest;
import com.aliyuncs.sts.model.v20150401.AssumeRoleResponse;
import com.google.gson.Gson;
import com.top.art.vo.PaperFileVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.*;

/**
 * @author zhubg
 * @title: AliyunOss
 * @projectName art-exam-manager
 * @description: TODO
 * @date 2020/10/4-15:13
 */
@Slf4j
@Component
public class AliyunOssUtil {

    @Value("${top.exam.accessKeyID}")
    private String accessKeyID;

    @Value("${top.exam.accessKeySecret}")
    private String accessKeySecret;

    @Value("${top.exam.bucketUrl}")
    private String bucketUrl;

    @Value("${top.exam.endPointUrl}")
    private String endPointUrl;

    @Value("${top.exam.tokenExpireTime}")
    private long tokenExpireTime;

    @Value("${top.exam.roleArn}")
    private String roleArn;

    @Value("${top.exam.roleSessionName}")
    private String roleSessionName;

    @Value("${top.exam.bucketName}")
    private String bucketName;

    @Value("${top.exam.endPoint}")
    private String endPoint;

    public PaperFileVO downloadFile(String objectName) {
        PaperFileVO paperFileVO = new PaperFileVO();
        OSS client = new OSSClientBuilder().build(endPoint, accessKeyID, accessKeySecret);
        String fileName = objectName.substring(objectName.lastIndexOf("/") + 1);
        File file = new File(fileName);
        ObjectMetadata metadata = client.getObject(new GetObjectRequest(bucketName, objectName), file);
        paperFileVO.setFile(file);
        paperFileVO.setFileName(fileName);
        paperFileVO.setContentType(metadata.getContentType());
        return paperFileVO;
    }

    public AssumeRoleResponse assumeRole() {

        //构建一个阿里云客户端，用于发起请求。
        //构建阿里云客户端时需要设置AccessKey ID和AccessKey Secret。
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyID, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        //构造请求，设置参数。
        AssumeRoleRequest request = new AssumeRoleRequest();
        request.setRegionId("cn-hangzhou");
        request.setRoleArn(roleArn);
        request.setRoleSessionName(roleSessionName);

        //发起请求，并得到响应。
        try {
            AssumeRoleResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
            return response;
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
        return null;
    }


}
